app.controller('NewPersonModalCtrl', ['$scope', '$modal', '$log', '$http', '$window', '$localStorage', '$state', '$location',
        function ($scope, $modal, $log, $http, $window, $localStorage, $state, $location) {
            $scope.items = ['item1', 'item2', 'item3'];
            //console.log($state.params);
            $scope.schooldata = [];
            var schooldata = $localStorage.layers_info;           
            for(var key in schooldata){
                if(key!="所有学校"){
                    var m = {};
                     m.school_id = schooldata[key].school_id;
                     m.school_name = schooldata[key].school_name;
                    $scope.schooldata.push(m);
                }                
               
            }
            
            var personnel_number,name,classroom,phone,type,id_number,school_id;
            var ModalInstanceCtrl = function ($scope, $modalInstance, items) {
                $scope.oo = {};
                $scope.items = items;
                $scope.selected = {
                    item: $scope.items[0]
                };
                $scope.ok = function () {
                    school_id = $scope.oo.school_id;
                    personnel_number = $scope.oo.personnel_number;
                    name = $scope.oo.name;   
                    classroom = $scope.oo.classroom;   
                    phone = $scope.oo.phone;   
                    type = $scope.oo.type;                                         
                    id_number = $scope.oo.id_number;   
                                      
                    $modalInstance.close($scope.selected.item);
                };

                $scope.cancel = function () {
                    $modalInstance.dismiss('cancel');
                };
            };
            $scope.open = function (size) {
                var modalInstance = $modal.open({
                    templateUrl: 'newpersonModalContent.html',
                    controller: ModalInstanceCtrl,
                    size: size,
                    resolve: {
                        items: function () {
                            return $scope.items;
                        }
                    }
                });
                modalInstance.result.then(function (selectedItem) {
                    
                    $http.post(
                        $scope.app.backendurl+"tbl-personnel/create",
                        {
                            'school_id':school_id,
                            'name': name,
                            'personnel_number': personnel_number,  
                            'class' :classroom,
                            'phone':phone,
                            'type':type,
                            'id_number':id_number,                      
                            'token':$localStorage.token             

                        }
                    ).success(function (data) {
                      //console.log(data);
                      if(data.code==0&&data.msg=='添加人员信息成功'){
                        alert(data.msg);
                        $window.location.reload('app/sys/person');                      

                      }
                       if(data.code==20001){
                         $location.path('access.signin');
                       }
                    }).error(function (data) {
                        alert(data);
                    })
                });
            };
        }])

app.controller('EditPersonModalCtrl', ['$scope', '$modal', '$log', '$http', '$window', '$localStorage', '$state', '$location',
        function ($scope, $modal, $log, $http, $window, $localStorage, $state, $location) {
            $scope.items = ['item1', 'item2', 'item3'];
            //console.log($state.params);
            var personnel_id,personnel_number,name,classroom,phone,type,id_number,version;
            var psnum,usname,class_room,uphone,utype,idnum;
            var ModalInstanceCtrl = function ($scope, $modalInstance, items) {
                $scope.oo = {};
                $scope.oo.personnel_number = psnum;
                $scope.oo.name = usname;                 
                $scope.oo.classroom = class_room;
                $scope.oo.phone = uphone;
                $scope.oo.type = utype;
                $scope.oo.id_number = idnum;
                $scope.items = items;
                $scope.selected = {
                    item: $scope.items[0]
                };
                $scope.ok = function () {                   
                    personnel_number = $scope.oo.personnel_number;
                    name = $scope.oo.name;
                    classroom = $scope.oo.classroom;
                    phone = $scope.oo.phone;
                    type = $scope.oo.type;
                    id_number = $scope.oo.id_number;                  
                                    
                    $modalInstance.close($scope.selected.item);
                };

                $scope.cancel = function () {
                    $modalInstance.dismiss('cancel');
                };
            };

            $scope.editrole = function(obj){
                //console.log(obj);
                personnel_id = obj.personnel_id;
                version = obj.version;
                psnum = obj.personnel_number;
                usname = obj.name; 
                class_room = obj.class;
                uphone = obj.phone;
                utype = obj.type;           
                idnum = obj.id_number;
                $scope.open();

            }
            $scope.open = function (size) {
                var modalInstance = $modal.open({
                    templateUrl: 'editpersonModalContent.html',
                    controller: ModalInstanceCtrl,
                    size: size,
                    resolve: {
                        items: function () {
                            return $scope.items;
                        }
                    }
                });
                modalInstance.result.then(function (selectedItem) {
                    
                    $http.post(
                        $scope.app.backendurl+"tbl-personnel/update",
                        {
                            'personnel_id': personnel_id,
                            'personnel_number': personnel_number,
                            'name': name,       
                            'class':classroom,
                            'phone':phone ,
                            'type':type,
                            'id_number':id_number,
                            'version': version,         
                            'token':$localStorage.token                         

                        }
                    ).success(function (data) {
                      //console.log(data);
                      if(data.code==0&&data.msg=="人员信息修改成功"){
                        alert(data.msg);
                        $window.location.reload('app/sys/person');

                      }
                       if(data.code==20001){
                         $location.path('access.signin');
                       }
                    }).error(function (data) {
                        alert(data);
                    })
                });
            };
        }])


app.controller('DelPersonModalCtrl', ['$scope', '$modal', '$log', '$http', '$window', '$location','$localStorage',
        function ($scope, $modal, $log, $http, $window, $location,$localStorage) {

            $scope.items = ['item1', 'item2', 'item3'];
            var personnel_id;            
            var ModalInstanceCtrl = function ($scope, $modalInstance, items) {

                $scope.oo = {};

                $scope.items = items;               
                $scope.selected = {
                    item: $scope.items[0]
                };
                $scope.ok = function () {

                    $modalInstance.close($scope.selected.item);
                };

                $scope.cancel = function () {
                    $modalInstance.dismiss('cancel');
                };
            };


            $scope.delperson = function (aid) {
                //console.log(aid);
                personnel_id = aid;             
                $scope.open();
            }

            $scope.open = function (size) {

                var modalInstance = $modal.open({
                    templateUrl: 'delpersonModalContent.html',
                    controller: ModalInstanceCtrl,
                    size: size,
                    resolve: {
                        items: function () {
                            return $scope.items;

                        }
                    }
                });

                modalInstance.result.then(function (selectedItem) {
                    $http.post(
                        $scope.app.backendurl+"tbl-personnel/delete",
                        {
                           
                            'personnel_id': personnel_id,                             
                            'token':$localStorage.token                   

                        }
                    ).success(function (data) {
                      //console.log(data);
                      if(data.code==0&&data.msg=="成功"){
                        alert(data.msg);
                        $window.location.reload('app/sys/person');

                      }
                       if(data.code==20001){
                         $location.path('access.signin');
                       }
                    }).error(function (data) {
                        alert(data);
                    })


                });
            };
        }])